import db from "@src/dataBase/db";
import Sequelize from "sequelize";
import File from "@src/models/file";
import Dict from "@src/models/dict";

const Article = db.define("article",
  {
    id: {type: Sequelize.STRING, primaryKey: true},
    name: {type: Sequelize.STRING},
    username: {type: Sequelize.STRING},
    content: {type: Sequelize.TEXT},
    cover_id: {type: Sequelize.STRING},
    region_id: {type: Sequelize.STRING},
    category_id: {type: Sequelize.STRING},
    recommend: {type: Sequelize.INTEGER},
    grounding: {type: Sequelize.INTEGER},
    visit_num:{type:Sequelize.INTEGER},
    created_at: {type: Sequelize.BIGINT},
    updated_at: {type: Sequelize.BIGINT},
  },
  {
    // 下划线命名
    underscored: true,
    // 阻止自动创建时间字段
    timestamps: false,
    // 表名
    tableName: "article",
  },
);

// 一对一，文章关联文件表
Article.belongsTo(File, {
  foreignKey: {
    field: 'cover_id',
  },
  as: "cover",
  targetKey: "id",
});

// 一对一，文章关联字典表
Article.belongsTo(Dict, {
  //本表的字段
  foreignKey: "category_id",
  as: "dict",
  // 目标字段
  targetKey: "id",
});

export default Article;